﻿using System;
using System.Linq;
using Codeplus.Dhruvtaata.Web.Models;
using Codeplus.Dhruvtaata.Web.Models.Entity;
using Codeplus.Dhruvtaata.Web.Models.EntityAction;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTests {

    [TestClass]
    public class StudentFixture2 {

        [TestMethod]
        public void ShouldAddNewStudentAndThenDelete() {
            using (var context = new DatabaseContext()) {
                var studentAction = new StudentCrudAction();
                studentAction.SetDataContext(context);
                var student = new Student() {
                    FirstName = "monica",
                    LastName = "sinha",
                    EmailAddress = "monica@codeplus.in",
                    DateOfBirth = DateTime.Now,
                    ContactAddress = new Address() {
                        Building = "krishna krupa",
                        City = "bangalore"
                    }
                };
                studentAction.Create(student);
                context.SaveChanges();
                studentAction.Delete(student);
                context.SaveChanges();
            }
        }

        [TestMethod]
        public void ShouldInsertSomeThenRetrieveThenClear() {
            using (var context = new DatabaseContext()) {
                var studentAction = new StudentCrudAction();
                studentAction.SetDataContext(context);
                var student = new Student() {
                    FirstName = "monica",
                    LastName = "sinha",
                    EmailAddress = "monica@codeplus.in",
                    DateOfBirth = DateTime.Now,
                    ContactAddress = new Address() {
                        Building = "krishna krupa",
                        City = "bangalore"
                    }
                };
                var s2 = new Student() {
                    FirstName = "monica",
                    LastName = "sinha",
                    EmailAddress = "monica@codeplus.in",
                    DateOfBirth = DateTime.Now,
                    ContactAddress = new Address() {
                        Building = "krishna krupa",
                        City = "bangalore"
                    }
                }; ;
                s2.FirstName = "p2"; s2.EmailAddress = "p2@q.com";
                var s3 = new Student() {
                    FirstName = "monica",
                    LastName = "sinha",
                    EmailAddress = "monica@codeplus.in",
                    DateOfBirth = DateTime.Now,
                    ContactAddress = new Address() {
                        Building = "krishna krupa",
                        City = "bangalore"
                    }
                }; ;
                s3.FirstName = "p3"; s3.EmailAddress = "p3@q.com";
                var s4 = new Student() {
                    FirstName = "monica",
                    LastName = "sinha",
                    EmailAddress = "monica@codeplus.in",
                    DateOfBirth = DateTime.Now,
                    ContactAddress = new Address() {
                        Building = "krishna krupa",
                        City = "bangalore"
                    }
                }; ;
                s4.FirstName = "p4"; s4.EmailAddress = "p4@q.com";
                var s5 = new Student() {
                    FirstName = "monica",
                    LastName = "sinha",
                    EmailAddress = "monica@codeplus.in",
                    DateOfBirth = DateTime.Now,
                    ContactAddress = new Address() {
                        Building = "krishna krupa",
                        City = "bangalore"
                    }
                };
                s5.FirstName = "p5"; s5.EmailAddress = "p5@q.com";
                studentAction.Create(student);
                studentAction.Create(s2);
                studentAction.Create(s3);
                studentAction.Create(s4);
                studentAction.Create(s5);
                context.SaveChanges();
                Func<Student, bool> whereClause = (s => s.EmailAddress.Contains("@q.com"));
                Func<Student, object> orderByClause = (s => s.PersonId);
                var students = studentAction.Retrieve(whereClause, orderByClause, 0, 10);
                Assert.AreEqual(4, students.ToList().Count);
                studentAction.Delete(student);
                studentAction.Delete(s2);
                studentAction.Delete(s3);
                studentAction.Delete(s4);
                studentAction.Delete(s5);
                context.SaveChanges();
            }
        }

        [TestMethod]
        public void AreStudentsEqual() {
            var s1 = new Student();
            s1.FirstName = "p";
            var s2 = new Student();
            s2.FirstName = "q";
            Assert.IsFalse(s1.Equals(s2));

            s1 = new Student();
            s2 = new Student();
            s1.EmailAddress = "z@c.com";
            s2.EmailAddress = "z@c.com";
            Assert.IsTrue(s1.Equals(s2));

            s1 = new Student();
            s2 = new Student();
            s1.FirstName = s2.FirstName = "qa";
            s1.LastName = s2.LastName = "alpha-beta";
            s1.EmailAddress = "z@c.com";
            s2.EmailAddress = "z@c.com";
            Assert.IsTrue(s1.Equals(s2));
        }
    }
}